import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import os
import sys
from config import Config
from torch import float32



#---------------------------------特殊处理---(如Bearing1_4.csv)--------------#
"""
for i in range(1,2449):
    j='0'*(5-len(str(i)))+str(i)
    A=pd.read_csv('DataSets/Bearing1_4_Ori/acc_{}.csv'.format(j),header=None)
    n_loc=np.array(A).shape[0]
    list_data=[]
    for i in range(n_loc):
        list_data+=[A.iloc[i][0].replace(';',',')]
    Data='\n'.join(list_data)
    with open('DataSets/Bearing1_4/acc_{}.csv'.format(j),'w+',encoding='utf-8') as f:
        f.write(Data)
"""
#---------------------------------------------------------#



#------------读取原始数据---------------#
path_data = Config.path_data
listdir = os.listdir(path_data)
listdir.sort()
# print(listdir)
data_BearingX_X = np.empty((0,2560),dtype=np.float16)
print(data_BearingX_X.shape)
for n, name in enumerate(listdir):
    if 'acc' in name:
        BearingX_X=pd.read_csv(path_data+name,header=None)[[4]]
        # print(BearingX_X)
        data_BearingX_X = np.append(data_BearingX_X,BearingX_X.values.reshape(1,-1),axis=0)
        print(data_BearingX_X.shape)
np.savetxt(path_data.replace('\\','_')+'.csv', data_BearingX_X, delimiter = ',')

'''
plt.plot(data_BearingX_X,linewidth=0.3)
#设置坐标轴刻度
# plt.ylim(-50,50)
plt.xticks(np.arange(0, 3000, 500))
plt.ylabel('振动程度(g)')
plt.xlabel('采样时间(x10s)')
plt.grid(linestyle="--")
plt.savefig('PHW12_sample1_3.svg')
plt.savefig('PHW12_sample1_3.png',bbox_inches='tight',dpi=800)
plt.show()
'''


'''
#----------数据归一化-----------#

std_data=pd.read_csv('val_Bearing1_3.csv',index_col=0)  #index_col=0把第一列作为行索引，默认第一行为列索引

n_col=len(list(std_data))  
print(n_col)
for i in range(n_col):
    x=abs(std_data[str(i)])
    std_data[str(i)]=(x-min(x))/(max(x)-min(x))
    
std_data.to_csv('val_Bearing1_3_std.csv')


plt.plot(std_data,linewidth=0.3)
#设置坐标轴刻度
plt.xticks(np.arange(0, 1900, 300))
plt.show()
'''

'''
#----------------训练集和测试集划分-----------#

#对列进行划分

train_test_data=pd.read_csv('Bearing1_3_std.csv',index_col=0)
n_col=len(list(train_test_data)) 

a=[str(i) for i in range(0,int(n_col*0.8))]
b=[str(i) for i in range(int(n_col*0.8),n_col)]

train_data=train_test_data[a]
test_data=train_test_data[b]

train_data.to_csv('Bearing1_3_train_data.csv')
test_data.to_csv('Bearing1_3_test_data.csv')


#按行划分数据集,随机划分
import random
train_test_data=pd.read_csv('Bearing1_3_std.csv',index_col=0)
n_row=np.array(train_test_data).shape[0] 
print(n_row)

test_select_sample=[]
train_select_sample=[]
for i in range(1,n_row+1):
    if i%5==0:
        test_select_sample.append(i)
    else: train_select_sample.append(i)

train_data=train_test_data.loc[train_select_sample]
test_data=train_test_data.loc[test_select_sample]



train_data.to_csv('Bearing1_3_train_data.csv')
test_data.to_csv('Bearing1_3_test_data.csv')



# train_data=test_data.iloc[[1,1,1]]  #iloc使用索引截取
# train_data=test_data.loc[[1,1,1]]  #使用标签截取
# print(train_data)
# size_input=np.array(train_data).shape[1]
# print(size_input)
'''